package com.example.app1.web.Test1Validation.validator;

import com.example.app1.web.Test1Validation.annotation.Username;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class UsernameValidator implements ConstraintValidator<Username, String> {

    private static final String regex = "^[a-zA-Z0-9一-龥]+$";

    @Override
    public void initialize(Username constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }



    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(!StringUtils.hasText(value)){
            return false;
        }
        return value.matches(regex);
    }
}
